package com.bnlr.lrparking.appui.paypwd;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.SetPayPasswordEvent;
import com.bnlr.lrparking.model.StallM;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * Created by hp on 2017/8/21.
 * 设置支付密码
 */

public class SetPayPasswordActivity extends BaseActivity {

    private TextView tvNavTitle,tv_setpaypwd,tvHint;
    private ClearEditText ed_setpaypwd;
    String paypwd="";
    private String resetPay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setpaypassword);
        Intent intent = getIntent();
        resetPay = intent.getStringExtra("reset");
        initView();
    }

    private void initView(){
        EventBus.getDefault().register(this);//
        tvNavTitle=(TextView)findViewById(R.id.tv_nav_title);
        tvHint=(TextView)findViewById(R.id.tvHint);
        if (resetPay != null) {
            tvNavTitle.setText("重置支付密码");
            tvHint.setText("为确保账户安全，请先输入登录密码");
        } else {
            tvNavTitle.setText("设置支付密码");
        }
        tv_setpaypwd=(TextView)findViewById(R.id.tv_setpaypwd);
        ed_setpaypwd=(ClearEditText)findViewById(R.id.ed_setpaypwd);
        tv_setpaypwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                paypwd=ed_setpaypwd.getText().toString();
                if (TextUtils.isEmpty(paypwd)) {
                    ed_setpaypwd.setShakeAnimation();
                    return;
                }
                getVerifyLoginPwd(paypwd);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SuccessEvent(SetPayPasswordEvent event){
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    //登录密码验证
    private void getVerifyLoginPwd(String pwd){
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "ValidUserPwd");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("pwd", pwd);

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<StallM>(baseContext, true, StallM.class) {
                        @Override
                        public void doWork(StallM data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (code.equals("1")) {
                                    if (resetPay == null) {
                                        Intent intent=new Intent(SetPayPasswordActivity.this,PayPasswordActivity.class);
                                        intent.putExtra("phonepwd",paypwd);
                                        intent.putExtra("tag","set");//设置支付密码
                                        startActivity(intent);
                                    } else {
                                        Intent intent=new Intent(SetPayPasswordActivity.this,ForgetPwdAuthenticationActivity.class);
                                        intent.putExtra("mima",paypwd);
                                        startActivity(intent);
                                    }

                                }else if (code.equals("0")){
                                    CommonUtil.showToask(SetPayPasswordActivity.this,obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }
}
